﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Sirenix.Utilities;
using UnityEngine;

namespace Gp.Scripts.Core
{
    public enum TurnState
    {
        // 结束轮
        RoundEnding,

        // 进攻轮
        Attack,

        // 防守轮
        Defense,
    }


    public class TurnResolveController : TurnResolverBase
    {
        /// 进行过回合的单位。在开始回合后更新。
        private HashSet<BattlePlayer> _unMovedPlayerSet;

        public override TransitionResult TransitionBattleStart()
        {
            _unMovedPlayerSet = new();
            ResetUnMovedPlayer();
            return new TransitionResult(Settle.HumanPlayer);
        }

        public override TransitionResult TransitionNextTurn()
        {
            if (_unMovedPlayerSet.Count == 0)
            {
                ResetUnMovedPlayer();
            }

            BattlePlayer ret = null;
            ret = _unMovedPlayerSet.FirstOrDefault(x => x is HumanPlayer) ?? _unMovedPlayerSet.FirstOrDefault();
            // ret 可能为空，此时会在BattleSystem中判断游戏结束
            _unMovedPlayerSet.Remove(ret);
            return new TransitionResult(ret);
        }

        private void ResetUnMovedPlayer()
        {
            _unMovedPlayerSet.Clear();
            _unMovedPlayerSet.AddRange(Settle.Players);
        }

        // public IEnumerator SwitchToNextTurn(BattleSystem system) {
        //     if (IsBlockEndTurn) yield break;
        //     // 阻塞结束回合，在当前处理完成前无法再次结束回合
        //     IsBlockEndTurn = true;
        //     
        //     if (system.CheckGameFinished()) yield break;
        //
        //     try {
        //         yield return TurnEndProcess(system);
        //         
        //         // 切换玩家
        //         TransitionResult transitionResult = _chain.ResolveTurn(Settle);
        //         Settle.CurrentPlayer = transitionResult.NextPlayer;
        //         
        //         Settle.GameState = new GameStateBlockInput();
        //
        //         _chain.NextState();
        //         
        //         system.SendEvent(new TurnResolveEvent(transitionResult.NextPlayer, CurState));
        //         
        //     }
        //     finally {
        //         IsBlockEndTurn = false;
        //     }
        // }


        // private IEnumerator TurnEndProcess(BattleSystem system) {
        //     // 标记回合结束
        //     foreach (var u in Settle.GetCurrentUnits()) {
        //         yield return u.TriggerTurnEnd(_chain.CurState());
        //     }
        //
        //     // 至少延迟0.1s
        //     yield return new WaitForSeconds(.1f);
        // }


        // // 下个回合开始处理
        // private IEnumerator TurnStartProcess(TransitionResult res) {
        //     // 标记单位回合开始 
        //     yield return res.PlayableUnits().Select(u => u.SmallTurnStart(_chain.CurState())).GetEnumerator();
        //
        //     // 设置当前 player
        //     Settle.CurrentPlayer = res.NextPlayer;
        //
        //     // 发送回合转换事件
        //     res.NextPlayer.Play(res);
        // }


        // public IEnumerator ResolveBattleStart(BattleSettleRoot settle) {
        //     // 比较两侧单位先攻点数大小，，决定回合顺序，当数值相等时主角方优先
        //     // 如果左侧先攻点数大于等于右侧，左侧先手
        //     var res = new TransitionResult(settle.HumanPlayer);
        //
        //     res.PlayableUnits = () =>
        //         settle.GetAllUnits().Where(u => u.PlayerNumber == res.NextPlayer.PlayerNumber).ToList();
        //     
        //     Global.Get<BattleSystem>().SendEvent(new TurnResolveEvent(res.NextPlayer, CurState));
        //     // 清空防御点
        //     foreach (var u in res.PlayableUnits()) {
        //         u.ComCost.ClearResource(CostField.Reaction);
        //     }
        //     // yield return TurnStartProcess(res);
        // }
    }
}